<template>
  <panel title="修改密码">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="原密码" prop="oldPwd">
        <el-input v-model="ruleForm.oldPwd" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
        <el-input v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="checkPass">
        <el-input v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button @click="submitForm('ruleForm')" type="primary">确认</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </div>
  </panel>
</template>

<script>
import { checkOldPwd, validatePass, validatePass2 } from "@/utils/validate";
import { editPwdReq } from "@/api/account.api.js";
export default {
  data() {
    return {
      labelPosition: "right",
      ruleForm: {
        pass: "",
        checkPass: "",
        oldPwd: "",
      },
      rules: {
        oldPwd: [
          { validator: checkOldPwd, trigger: "blur" },
          // {
          //   pattern: /^[a-zA-Z\d]+$/,
          //   message: "请输入正确格式密码",
          //   trigger: "change",
          // },
        ],
        pass: [
          { validator: validatePass.bind(this), trigger: "blur" },
          // {
          //   pattern: /^[a-zA-Z\d]+$/,
          //   message: "请输入正确格式密码",
          //   trigger: "change",
          // },
        ],
        checkPass: [
          { validator: validatePass2.bind(this), trigger: "blur" },
          // {
          //   pattern: /^[a-zA-Z\d]+$/,
          //   message: "请输入正确格式密码",
          //   trigger: "change",
          // },
        ],
      },
    };
  },
  methods: {
    // async submit() {
    //   if (this.ruleForm.newPwd == this.ruleForm.reNewPwd) {
    //     const res = await axios.get(
    //       `/users/checkoldpwd?oldPwd=${this.ruleForm.oldpwd}`
    //     );
    //     console.log(res);
    //     if (res.data.code == "00") {
    //       axios
    //         .post("/users/editpwd", { newPwd: this.ruleForm.newPwd })
    //         .then((res1) => {
    //           console.log(res1);
    //           if (res1.data.code == 0) {
    //             this.$message({
    //               message: res1.data.msg,
    //               type: "success",
    //             });
    //             this.$router.push("/login");
    //           } else {
    //             this.$message.error(res.data.msg);
    //           }
    //         });
    //     } else {
    //       this.$message.error(res.data.msg);
    //     }
    //   } else {
    //     this.$message.error("两次密码输入不一致");
    //   }
    // },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(valid);
          // console.log(typeof String(this.ruleForm.pass));
          editPwdReq(this.ruleForm.pass).then((res) => {
            console.log(res);
            this.$message({
              message: res.data.msg,
              type: "success",
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.btn {
  margin-left: 100px;
}
</style>